package com.chengshi.shop.service.order.impl;

import com.chengshi.shop.dao.order.OrderHistoryMapper;
import com.chengshi.shop.dao.order.OrderMapper;
import com.chengshi.shop.model.order.ExpressInfo;
import com.chengshi.shop.model.order.ExpressQueryResult;
import com.chengshi.shop.model.order.Order;
import com.chengshi.shop.model.order.OrderHistory;
import com.chengshi.shop.service.order.OrderHistoryService;
import com.chengshi.shop.util.DateFormatUtil;
import com.chengshi.shop.util.EnumUtil;
import com.chengshi.shop.util.ExpressUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author xuxinlong
 * @version 2017年07月28日
 */
@Service
public class OrderHistoryServiceImpl implements OrderHistoryService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderHistoryMapper orderHistoryMapper;

    /**
     * 获取订单历史记录
     * @param orderId
     * @return
     */
    @Override
    public List<OrderHistory> getListByOrderId(Integer orderId) throws Exception{
        Order order = orderMapper.selectByPrimaryKey(orderId);
        List<OrderHistory> list = orderHistoryMapper.getListByOrderId(orderId);
        //未签收时查询物流接口，已签收查询数据库
        if (!order.getIsSign()) {
            if (StringUtils.isNotBlank(order.getExpressCode()) && StringUtils.isNotBlank(order.getExpressNum())) {
                ExpressQueryResult result = ExpressUtil.getExpress(order.getExpressCode(), order.getExpressNum());
                //查询成功
                if (result.getSuccess()) {
                    Boolean isSign = false;
                    if (Objects.equals(result.getState(), EnumUtil.EXPRESS_STATUS.签收.getValue().toString())) {
                        //物流已签收保存到数据库
                        order.setIsSign(true);
                        orderMapper.updateByPrimaryKeySelective(order);
                        isSign = true;
                    }
                    for (ExpressInfo expressInfo : result.getTraces()) {
                        OrderHistory orderHistory = new OrderHistory();
                        orderHistory.setOrderId(orderId);
                        orderHistory.setCreateTime(DateFormatUtil.formatDate(expressInfo.getAcceptTime()));
                        orderHistory.setContent(expressInfo.getAcceptStation());
                        orderHistory.setOpName("系统");
                        if (isSign) {
                            orderHistoryMapper.insertSelective(orderHistory);
                        }
                        list.add(0, orderHistory);
                    }
                } else {
                    OrderHistory orderHistory = new OrderHistory();
                    orderHistory.setOrderId(orderId);
                    orderHistory.setCreateTime(new Date());
                    orderHistory.setContent(result.getReason());
                    orderHistory.setOpName("系统");
                    list.add(orderHistory);
                }
            }
        }
        return list;
    }
}
